package com.compass.controller.tool;

import com.compass.service.DubboService;
import com.compass.vo.Dubbo;
import com.compass.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/3/17
 * Time: 14:27
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/dubbo")
public class DubboController {

    @Autowired
    DubboService dubboService;

    @RequestMapping("/dubboPage")
    public String dubboPage(ModelMap modelMap){
        modelMap.addAttribute("zkAddress","47.105.55.243:2181");
        return "/tool/dubbo";
    }
    @RequestMapping("/resolveIp")
    public @ResponseBody
    ResponseMessage resolveIp(String zkAddress, String interfaceName){
        if(StringUtils.isEmpty(zkAddress)){
            return ResponseMessage.errorResponse("zookeeper地址不能为空");
        }
        if(StringUtils.isEmpty(interfaceName)){
            return ResponseMessage.errorResponse("接口名称不能为空");
        }
       return dubboService.resolveIp(zkAddress,interfaceName);
    }

    @RequestMapping("/resolveMethod")
    public @ResponseBody
    ResponseMessage resolveMethod(String interfaceName,String ip){
        if(StringUtils.isEmpty(interfaceName)){
            return ResponseMessage.errorResponse("接口名称不能为空");
        }
        if(StringUtils.isEmpty(ip)){
            return ResponseMessage.errorResponse("ip不能为空");
        }
        return dubboService.resolveMethod(interfaceName,ip);
    }
    @RequestMapping("/methodParam")
    public @ResponseBody ResponseMessage methodParam(Dubbo dubbo){
        return dubboService.methodParam(dubbo);
    }
    @RequestMapping("/invoke")
    public @ResponseBody String invoke(Dubbo dubbo){
        return dubboService.invoke(dubbo);
    }
    @RequestMapping("/invokeForTestCase")
    public @ResponseBody String invokeForTestCase(HttpSession httpSession, String param){
        Integer  interfaceId=(Integer) httpSession.getAttribute("interfaceId");
        return dubboService.invokeForTestCase(interfaceId,param);
    }
}
